A very simple bounded double-ended queue/stack used for caching a list of values. Overrides a few methods from Array so that if more than a certain number of elements are added, entries at the back of the array will be popped off
Initialize a queue with a set length. The backing array is not resized to match this size.
# File lib/bounded_queue.rb, line 15 def initialize(max_size) super() @max_size = max_size end
Pushes an element onto the front of the “queue”, removing the last element in the queue if necessary.
# File lib/bounded_queue.rb, line 23 def push(element) pop if size + 1 >= @max_size push_internal(element) end
Pushes an element onto the front of the “stack”, removing the first element in the stack if necessary.
# File lib/bounded_queue.rb, line 31 def unshift(element) shift if size + 1 >= @max_size unshift_internal(element) end